package com.longrise.android.byjk.plugins.poster.posterdetail.assist;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by godliness on 2017/9/22.
 * 压缩方式分为：
 * ndk压缩  JPEG原生引擎
 * 质量压缩
 * 尺寸压缩
 * 采样率压缩
 */

public class ImgCompress {

    /**
     * 质量压缩
     *
     * @param bmp           源图片
     * @param compressValue 0-100 100为不压缩
     * @param outFile       输出的路径
     */
    public static void imgQualityCompress(Bitmap bmp, int compressValue, File outFile, int applySize) {
        ByteArrayOutputStream baos = null;
        FileOutputStream fos = null;
        try {
            baos = new ByteArrayOutputStream();
            bmp.compress(Bitmap.CompressFormat.JPEG, compressValue, baos);
            while ((baos.toByteArray().length / 1024) > applySize){
                baos.reset();
                compressValue -= 5;
                bmp.compress(Bitmap.CompressFormat.JPEG, compressValue, baos);
            }
            fos = new FileOutputStream(outFile);
            fos.write(baos.toByteArray());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (fos != null) {
                    fos.flush();
                    fos.close();
                }
                if(baos != null){
                    baos.flush();
                    baos.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 采样率压缩
     */
    public static void imgSamplingRateCompress() {

    }

    /**
     * 尺寸压缩
     *
     * @param bmp           源图片
     * @param compressValue 尺寸缩放比率 尺寸压缩倍数,值越大，图片尺寸越小   1表示不压缩
     * @param outFile       输出路径
     */
    public static void imgDimensionCompress(Bitmap bmp, int compressValue, File outFile) {
        int width = bmp.getWidth();
        int height = bmp.getHeight();

        int compressWidth = width / compressValue;
        int compressHeight = height/ compressValue;

        Bitmap newBitmap = Bitmap.createBitmap(compressWidth, compressHeight, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(newBitmap);
        Rect rect = new Rect(0, 0, compressWidth, compressHeight);
        canvas.drawBitmap(bmp, null, rect, null);

        ByteArrayOutputStream baos;
        FileOutputStream fos = null;
        try {
            baos = new ByteArrayOutputStream();
            newBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            fos = new FileOutputStream(outFile);
            fos.write(baos.toByteArray());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.flush();
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    /**
     * JPEG原生处理引擎
     * 05年基于JPEG引擎的二次开发skia处理引擎，便于浏览器处理
     * Android 07年面世，基于skia引擎，但是去除了一个重要算法，哈夫曼算法。采用定长算法
     * 主要是因为当时硬件系统性能较差
     */
    public static void imgJPEGNativeCompress() {

    }
}
